package io.renren.modules.jxgkteacher.controller;

import io.renren.common.utils.R;

import io.renren.modules.jxgkphone.service.ClassAttendanceService;
import io.renren.modules.jxgkphone.tempEntity.AttendanceStateInfo;
import io.renren.modules.jxgkphone.tempEntity.TeacherAttendanceStateEntity;

import io.renren.modules.jxgkteacher.dao.TeacherCourseMapper;

import io.renren.modules.jxgkteacher.entity.TeacherCourseEntity;
import io.renren.modules.jxgkteacher.service.TeacherClassAttendanceService;
import io.renren.modules.jxgkteacher.service.TeacherCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/TeacherClassAttendance")
public class TeacherClassAttendanceController {
    @Autowired
    ClassAttendanceService classAttendanceService;
    @Autowired
    TeacherCourseMapper teacherCourseMapper;
    @Autowired
    TeacherCourseService teacherCourseService;
    @Autowired
    TeacherClassAttendanceService teacherClassAttendanceService;

    /**1 修改  修改多个班级的学生的考勤状态
     * 输入：一个json，多个学生的id和状态信息
     * 返回：所有人的状态
     */
    @RequestMapping("/Insert")
    public R updateTeacherState(@RequestBody Map<String,List<TeacherAttendanceStateEntity>> attendState){
        String isAttendance="0";
        for(Object obj:attendState.entrySet()){
            Map.Entry entry= (Map.Entry) obj;
            String cid=entry.getKey()+"";
            List<String> state=teacherClassAttendanceService.modifyState(cid);
            if(state.isEmpty()){
                //首次写入 insert
                for(TeacherAttendanceStateEntity t:(List<TeacherAttendanceStateEntity>)entry.getValue()){
                    t.setIsAttendanceTeacher("1");
                    teacherClassAttendanceService.InsertState(t);
                }
                isAttendance="1";
            } else if(state.get(0).equals("1")){
                //第一次修改
                //先删除
                teacherClassAttendanceService.deleteClassState(cid);
                //再插入
                for(TeacherAttendanceStateEntity t:(List<TeacherAttendanceStateEntity>)entry.getValue()){
                    t.setIsAttendanceTeacher("2");
                    teacherClassAttendanceService.InsertState(t);
                }
                isAttendance="2";
            } else {
                return   R.error("您的数据有误");
            }
        }
        return   R.ok().put("isAttendence",isAttendance);
    }
    @RequestMapping("/Details")
    public Map<String,Object> findStudentListDayCourse(@RequestBody Map<String,String> param){
        List<TeacherCourseEntity> teacherCourseEntities=teacherCourseService.FindTeacherCourseByTime(param.get("teacherId"),param.get("courseTime"));
        Map<String,Object> details=new HashMap<>();
        for(TeacherCourseEntity t:teacherCourseEntities){
            List<AttendanceStateInfo> attendanceStateInfos=classAttendanceService.findClassAttendanceTeacher(t.getClassCourseId(),t.getClassName());
            for(AttendanceStateInfo A:attendanceStateInfos){

                if(A.getIsAttendanceTeacher()==null){
                    A.setIsAttendanceTeacher("0");

                }


            }
            details.put(t.getClassName(),attendanceStateInfos);
        }
        return details;
    }





}
